/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package maxent.mr.mapred;

import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

/**
 *
 * @author hadoop
 */
public class TagTokenMapper
        extends Mapper <LongWritable, Text, Text, Text>{

    private String tagSeparator;

    @Override
    public void setup(Context context){

        Configuration conf = context.getConfiguration();
        tagSeparator = conf.get("tagseparator");
    }

    @Override
    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{

        // fetching line from training data
        String line = value.toString();
        String word;
        String tag;
        
        StringTokenizer tokenizer = new StringTokenizer(line);
        while (tokenizer.hasMoreTokens()){
            String token = tokenizer.nextToken();
        
            int indexUnd = token.lastIndexOf(tagSeparator);
            word = token.substring(0, indexUnd).intern();
            tag = token.substring(indexUnd + 1).intern();            

            context.write(new Text(tag), new Text(word));
        }
        
    }

}
